import { EmployeeLoginComponent } from './../employeeLogin/employeeLogin.component';
import { SyncComponent } from './../dataSync/sync.component';
import { SetupComponent } from './../setup/setup.component';
import { PrinterComponent } from './../device/printer/printer.component';
import { TableComponent } from './../table/table.component';
import { NavController } from 'ionic-angular';
import { TableService } from './../../service/table.service';
import { ConfirmParams } from './../../component/primeng/confirm/confirm.module';
import { NavigateService } from './../../service/navigate.service';
import { Component, OnInit, animate } from '@angular/core';
import { MdDialog } from '@angular/material';
import { MessageBoxService, MessageResult, GrowlMessageType } from "../../component/index";
import { Authentication, SyncDataService } from "../../service/index";
//import { ViewBase } from "../viewBase";

@Component({
    //moduleId: module.id,
    selector: 'div[app-home]',
    templateUrl: "./home.component.html",
    styleUrls: ['./home.component.scss']
})
export class HomeComponent implements OnInit {
    constructor(
        private router: NavigateService,
        private syncSvr: SyncDataService,
        private authSvr: Authentication,
        private dialog: MdDialog,
        private tableSrv: TableService,
        public navCtrl: NavController
    ) { }

    user;
    store;
    terminal;
    cloudHostName;
    ipAddress = "";
    itemData = [
        {
            name: '打卡',
            pngSrc: './assets/images/punchcard.png',
            pngGraySrc: './assets/images/punchcard_gray.png',
            disabled: true
        },
        {
            name: '台桌',
            route: 'table',
            pngSrc: './assets/images/table.png',
            component: TableComponent,
            pngGraySrc: '',
            params: [''],
            disabled: false
        },
        {
            name: '订单',
            pngSrc: './assets/images/order.png',
            pngGraySrc: './assets/images/order_gray.png',
            disabled: true
        },
        {
            name: '服务',
            pngSrc: './assets/images/service.png',
            pngGraySrc: './assets/images/service_gray.png',
            disabled: true
        },
        {
            name: '通知',
            pngSrc: './assets/images/notification.png',
            pngGraySrc: './assets/images/notification_gray.png',
            disabled: true
        },
        {
            name: '业绩',
            pngSrc: './assets/images/performance.png',
            pngGraySrc: './assets/images/performance_gray.png',
            disabled: true
        }
    ];
    tableZones = [];
    async ngOnInit() {
        this.cloudHostName = this.syncSvr.CloudHost + ":" + this.syncSvr.CloudPort;
        this.user = this.syncSvr.CurrentUser;
        this.store = this.syncSvr.CurrentStore;
        this.terminal = this.syncSvr.Terminal;
        this.ipAddress = this.syncSvr.LDCAddress;
        this.tableZones = await this.tableSrv.GetTableZoneList();
        if (this.tableZones && this.tableZones.length) {
            let item = this.itemData.find((data) => {
                return (<any>data).route == 'table';
            });
            if (item) {
                (<any>item).params = { zoneId: this.tableZones[0].id };
            }
        }
    }

    redirectTo(item) {
        if (item.disabled) {
            return;
        }
        let routeArr = [item.route];
        if (item.params) {
            // routeArr = routeArr.concat(item.params);
            this.navCtrl.setRoot(item.component, item.params, { animate: false });
        } else {
            this.navCtrl.setRoot(item.component,{}, { animate: false });
        }
        // this.router.navigate(routeArr);
    }

    goPrinter() {
        // this.router.navigate(['printer']);
        this.navCtrl.push(PrinterComponent,{}, { animate: false });
    }

    goSetting() {
        // this.router.navigate(['setup']);
        this.navCtrl.push(SetupComponent,{}, { animate: false });
    }

    goSync() {
        // this.router.navigate(['dataSync']);
        this.navCtrl.setRoot(SyncComponent, {},{ animate: false });
    }

    async logout() {
        let confirmParams: ConfirmParams = {
            content: "确认退出？"
        }
        let result = await MessageBoxService.Current.Show(this.dialog, confirmParams);
        if (result == MessageResult.Ok) {
            this.authSvr.employeeLogout();
            // this.router.navigate(["employeeLogin"]);
            this.navCtrl.setRoot(EmployeeLoginComponent);
        }
    }
}
